package com.example.myapplication;
//MainActivity
//app-src-main-java-com.example.myapplication-Second.java(class)
import android.content.ContentValues;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.Toast;

import com.google.android.material.bottomnavigation.BottomNavigationView;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;


import java.io.Console;
import java.util.ArrayList;
import java.util.List;   //这个需要导入

public class Second extends AppCompatActivity {
    //----------------------------------------------private area-------------------------
    private MyDatabaseHelper dbHelper;
    //    private ActivityMainBinding binding;
    private String[] data={"a"};
    private EditText edit0;
    private EditText edit1;
    //-------------------------------------------end of private area-------------------------------
    //---------------------------------------------area of function--------------------------
    public String[] addElementToStringArray(String[] oldArray, String newElement) {   //这个是动态添加元素
        // 创建新的String数组
        String[] newArray = new String[oldArray.length + 1];
        // 复制原有的String数组
        System.arraycopy(oldArray, 0, newArray, 0, oldArray.length);
        // 添加新的String元素
        newArray[oldArray.length] = newElement;
        return newArray;
    }

    public  String[] returnzero() {        //这个是返回一个长度为0的string数组
        String[] arrNew = new String[0];
        return arrNew;
    }

    //---------------------------------------------end of area of function------------
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.second);

        //-------------------------------事件1定义区-------------------------------------------------
        dbHelper=new MyDatabaseHelper(this,"BookStore.dp",null,2);  //version这里相当于数据库的版本
        Button button1=(Button) findViewById(R.id.button1);
        Button button2=(Button)findViewById(R.id.button2);
        //Button button3=(Button)findViewById(R.id.button3);
        Button button4=(Button)findViewById(R.id.button4);
        edit0 =(EditText) findViewById(R.id.edittext0);
        ListView listView=(ListView) findViewById(R.id.list_view);
        ImageView nextpage=(ImageView) findViewById(R.id.nextpage);
        //-------------------以上是事件1定义区----------------------------------

        //------------------------初始化查询，更新索引部分----------------------------
        SQLiteDatabase db=dbHelper.getWritableDatabase();
        data=returnzero();  //清零data
        //查询Book中的所有数据
        Cursor cursor=db.query("Book",null,null,null,null,null,null);
        if(cursor.moveToFirst()){
            do{
                //遍历Cursor对象，取出数据并打印
                String name=cursor.getString(cursor.getColumnIndex("name"));

                Log.d("MainActivity23","Book is "+name);
//                Toast.makeText(MainActivity.this,name,Toast.LENGTH_SHORT).show();
                data=addElementToStringArray(data,name);   //还要记得赋值，不然没有反应
            }while (cursor.moveToNext());

        }
        //-----------循环结束--------------
        ArrayAdapter<String> adapter =new ArrayAdapter<String>(Second.this,
                android.R.layout.simple_list_item_1,data);
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                // 获取被点击的item的数据
                String item = (String) adapter.getItem(position);
                // 执行你需要的操作
                AlertDialog.Builder dialog =new AlertDialog.Builder(Second.this);
                dialog.setTitle("提示");
                dialog.setMessage("确定删除？");
                dialog.setCancelable(false);//这个应该是去掉关闭栏的意思

                dialog.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
//                        这是点击提示栏确定后的方法

                    }
                });
                dialog.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
//                        这是点击提示栏确定后的方法
                        //----------------------------------绑定在listview里的删除----------------------------------------
                        //-------------------------------删除部分-------------------------------------
                        String inputText=item;
                        SQLiteDatabase db=dbHelper.getWritableDatabase();
//                db.delete("Book","pages>?",new String[]{"500"});
                        db.delete("Book","name=?",new String[]{inputText});
                        //-------------------------------end of 删除部分------------------------------

                    }
                });
                dialog.show();
            }
        });

        //------------------------end of 初始化查询更新索引部分--------------------------
        //---------------------以下事件2的事件---------------------------------
        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dbHelper.getWritableDatabase();
            }
        });
        //--------------------------------以上是事件2的事件区--------------------------------------------
        //---------------------------------事件3定义区----------------------------------------
        button2.setOnClickListener(new View.OnClickListener() {   //新增数据
            @Override
            public void onClick(View v) {

                //------------------------新增部分-----------------------------
                String inputText=edit0.getText().toString();
                if(inputText!="") {
                    SQLiteDatabase db = dbHelper.getWritableDatabase();
                    ContentValues values = new ContentValues();
                    //开始组装第一题数据
                    values.put("name", inputText);

                    db.insert("Book", null, values);
                    values.clear();
                    //开始组装第二条数据
                    //                values.put("name","The Lost Symbol");
                    //                db.insert("Book",null,values);
                    //                values.clear();
                }
                //------------------------end of 新增部分-----------------------------
                //------------------------查询，更新索引部分----------------------------
                SQLiteDatabase db=dbHelper.getWritableDatabase();
                data=returnzero();  //清零data
                //查询Book中的所有数据
                Cursor cursor=db.query("Book",null,null,null,null,null,null);
                if(cursor.moveToFirst()){
                    do{
                        //遍历Cursor对象，取出数据并打印
                        String name=cursor.getString(cursor.getColumnIndex("name"));

                        Log.d("MainActivity23","Book is "+name);
//                        Toast.makeText(MainActivity.this,name,Toast.LENGTH_SHORT).show();
                        data=addElementToStringArray(data,name);   //还要记得赋值，不然没有反应
                    }while (cursor.moveToNext());

                }
                //-----------循环结束--------------
                ArrayAdapter<String> adapter =new ArrayAdapter<String>(Second.this,
                        android.R.layout.simple_list_item_1,data);
                listView.setAdapter(adapter);
                //-------------------------------添加点击事件--------------------------------------
                listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        // 获取被点击的item的数据
                        String item = (String) adapter.getItem(position);
                        // 执行你需要的操作
                        AlertDialog.Builder dialog =new AlertDialog.Builder(Second.this);
                        dialog.setTitle("提示");
                        dialog.setMessage("确定删除？");
                        dialog.setCancelable(false);//这个应该是去掉关闭栏的意思

                        dialog.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
//                        这是点击提示栏确定后的方法

                            }
                        });
                        dialog.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
//                        这是点击提示栏确定后的方法
                                //----------------------------------绑定在listview里的删除----------------------------------------
                                //-------------------------------删除部分-------------------------------------
                                String inputText=item;
                                SQLiteDatabase db=dbHelper.getWritableDatabase();
//                db.delete("Book","pages>?",new String[]{"500"});
                                db.delete("Book","name=?",new String[]{inputText});
                                //-------------------------------end of 删除部分------------------------------

                            }
                        });
                        dialog.show();
                    }
                });
                //-----------------------------上面是添加点击事件----------------------------------------------------

                //------------------------end of 查询更新索引部分--------------------------
            }
        });

        //----------------------------------以上是事件3定义区-------------------------------------//
        //----------------------------------事件4定义区--------------------------------------------

        //-----------------------------------以上是事件4定义区-------------------------------------
        //----------------------------------事件5定义区-------------------------------------------
        button4.setOnClickListener(new View.OnClickListener() {  //删除数据
            @Override
            public void onClick(View v) {
                //-------------------------------删除部分-------------------------------------

                //-------------------------------end of 删除部分------------------------------
                //------------------------查询，更新索引部分----------------------------

                data=returnzero();  //清零data
                //查询Book中的所有数据
                Cursor cursor=db.query("Book",null,null,null,null,null,null);
                if(cursor.moveToFirst()){
                    do{
                        //遍历Cursor对象，取出数据并打印
                        String name=cursor.getString(cursor.getColumnIndex("name"));

                        Log.d("MainActivity23","Book is "+name);
//                        Toast.makeText(MainActivity.this,name,Toast.LENGTH_SHORT).show();
                        data=addElementToStringArray(data,name);   //还要记得赋值，不然没有反应
                    }while (cursor.moveToNext());

                }
                //-----------循环结束--------------
                ArrayAdapter<String> adapter =new ArrayAdapter<String>(Second.this,
                        android.R.layout.simple_list_item_1,data);
                listView.setAdapter(adapter);
                //-------------------------------添加点击事件--------------------------------------
                listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        // 获取被点击的item的数据
                        String item = (String) adapter.getItem(position);
                        // 执行你需要的操作
                        AlertDialog.Builder dialog =new AlertDialog.Builder(Second.this);
                        dialog.setTitle("提示");
                        dialog.setMessage("确定删除？");
                        dialog.setCancelable(false);//这个应该是去掉关闭栏的意思

                        dialog.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
//                        这是点击提示栏确定后的方法

                            }
                        });
                        dialog.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
//                        这是点击提示栏确定后的方法
                                //----------------------------------绑定在listview里的删除----------------------------------------
                                //-------------------------------删除部分-------------------------------------
                                String inputText=item;
                                SQLiteDatabase db=dbHelper.getWritableDatabase();
//                db.delete("Book","pages>?",new String[]{"500"});
                                db.delete("Book","name=?",new String[]{inputText});
                                //-------------------------------end of 删除部分------------------------------

                            }
                        });
                        dialog.show();
                    }
                });
                //-----------------------------上面是添加点击事件----------------------------------------------------
                //------------------------end of 查询更新索引部分--------------------------
            }
        });
        //-----------------------------------以上是事件5定义区--------------------------------------
        //-------------------------------------area 6--------------------------------------------
        nextpage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(Second.this,MainActivity.class);
                startActivity(intent);
            }
        });

        //----------------------------------end of area6----------------------------------------

    }

    //----------------------------------oncreate外事件1定义区--------------------------------------------

    //----------------------------------以上是oncreate外事件1定义区--------------------------------------------
}